<?php

require_once('load.php');
$smarty->assign("s1","login");

if(LOGGED == false){
	if(isset($_POST['username']) && isset($_POST['userpass'])){
		if(count($sql->QueryGetFetch("SELECT * FROM `uzytkownicy` WHERE `login`='".$_POST['username']."' AND `haslo`='".md5($_POST['userpass'])."'"))){
			$ck = md5(time());
			$sql->query("UPDATE `uzytkownicy` SET `hash_code` = '".$ck."' WHERE `login`='".$_POST['username']."'");
			setcookie("admin_cookie_user", $_POST['username']);
			setcookie("admin_cookie_hashcode", $ck);
			//header('Location: ./index.php');
			//echo 'SELECT * FROM "Uzytkownicy" WHERE "user_name"=\''.$_POST['username'].'\' AND "USER_COOKIE_HASH"=\''.($ck).'\'';
			header('Location: ./index.php?action=success&k=zalogowano');
		}else{
			header('Location: ./index.php?action=failed&k=logowanie');
		}
	}
}else{
	
	//header('Location: ./index.php');
}

if(isset($_GET['action'])){
	switch ($_GET['action']) {
		case 'failed':
		
			if(isset($_GET['k'])){
				if($_GET['k'] == 'logowanie'){
					$smarty->assign("komunikat","Zły login lub hasło");
				}else{
					$smarty->assign("komunikator","Wstąpił błąd");
				}
			}else{
				$smarty->assign("komunikat","Wstąpil błąd");
			}
			
			break;
		case 'success':
		
			if(isset($_GET['k'])){
				if($_GET['k'] == 'zalogowano'){
					$smarty->assign("komunikat","Zalogowano");
				}else{
					$smarty->assign("komunikator","Pomyslnie zakonczono");
				}
			}else{
				$smarty->assign("komunikat","Pomyslnie zakonczono");
			}
			break;

	}
}
$system->dodaj_js('skrypty.js');
$smarty->display('templates/index.tpl');
?>